package moodish.server;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GraphicsConfiguration;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
import javax.swing.text.DefaultCaret;

public class ServerGUI {
	private JFrame frame = new JFrame();
	private JTextArea log = new JTextArea();
	private JScrollPane pane;

	public ServerGUI() {
		build();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(new Dimension(390, 300));
		frame.setResizable(false);
		frame.setTitle("Moodish Server");
		frame.setVisible(true);
	}

	private void build() {
		log.setEditable(false);

		pane = new JScrollPane(log);
		pane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		pane.setSize(160, 180);

		GraphicsConfiguration gc = frame.getGraphicsConfiguration();
		frame.setLocation((int) ((gc.getBounds().width / 2) - 180),
				(int) ((gc.getBounds().height / 2) - 180));

		DefaultCaret caretReceiver = (DefaultCaret) log.getCaret();
		caretReceiver.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);

		frame.getContentPane().setLayout(new BorderLayout());
		frame.getContentPane().add(BorderLayout.CENTER, pane);
	}

	public void addText(String text) {
		if (log.getText() != null && log.getText().trim().length() == 0)
			log.append(text);
		else
			log.append("\n" + text);
	}

}
